﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using StockCrawlerLib.DAL;
using StockCrawlerLib;
using CrawlerController;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace StockCrawlerWPF
{
    /// <summary>
    /// Interaction logic for ImportData.xaml
    /// </summary>
    public partial class ImportData : Window
    {
        private HistoryRepository _historyRepository = new HistoryRepository();
        private StockRepository _stockRepository = new StockRepository();
        public ImportData()
        {
            InitializeComponent();
            dtStart.DateTime = DateTime.Now;
            dtEnd.DateTime = DateTime.Now;
            RefreshGrid();
        }
        public void RefreshGrid()
        {
            gridHistory.ItemsSource = _historyRepository.Select_MaxNgayGiaoDich();
            //gridHistory.Columns["exchange"].GroupIndex = 0;
        }
       

        //private void CafefImport()
        //{
        //    BaseCrawler objCafefCrawler = new CafefCrawler();
        //    StockRepository obj = new StockRepository();
        //    List<stock> stockList = obj.GetStockList().ToList();
        //    foreach (stock objStock in stockList)
        //    {
        //        lblCode.Content = objStock.code;
        //        //Lấy thông tin stock

        //        //Lấy dữ liệu price
        //        objCafefCrawler.ImportPriceHisotry(objStock.code, dtStart.DateTime);

        //        //Lấy dữ liệu order
        //        objCafefCrawler.ImportOrderHisotry(objStock.code, dtStart.DateTime);
        //    }
        //}

        private void FptsImport()
        {
            BaseCrawler objCrawler = new FptsCrawler();
            DateTime dt = dtStart.DateTime;
            while (dt <= dtEnd.DateTime)
            {
                if (dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday)
                    continue;

                //Lấy dữ liệu price
                objCrawler.ImportPriceHisotry("", dt);

                //Lấy dữ liệu đặt lệnh
                objCrawler.ImportOrderHisotry("", dt);

                //Lấy dữ liệu giao dịch nước ngoài
                objCrawler.ImportForeignHistory(dt);

                dt = dt.AddDays(1);

                //Lấy dữ liệu giao dịch nước ngoài
                objCrawler.ImportForeign2History(dtStart.DateTime);
            }
            gridViewLog.ItemsSource = SystemConfig.SystemLog;
        }
        private void btnImportHistory_Click(object sender, RoutedEventArgs e)
        {
            FptsImport();
        }


        private void btnRefreshGrid_Click(object sender, RoutedEventArgs e)
        {
            RefreshGrid();
        }

        private void btnView_Click(object sender, RoutedEventArgs e)
        {
            gridHistory.ItemsSource = _historyRepository.SelectByNgayGiaoDich(dtStart.DateTime);
            //gridHistory.Columns["exchange"].GroupIndex = 0;

            lblCount.Content = gridHistory.VisibleRowCount.ToString() + " items";
            //gridHistory.ItemsSource = new LinqServerModeSource()
            //{
            //    KeyExpression = "id_stock",
            //    QueryableSource = _historyController.Select_CustomNgayGiaoDich(dtStart.DateTime)
            //};
        }

        private void btnCapNhatGiaTC_Click(object sender, RoutedEventArgs e)
        {
            //BaseCrawler objCafefCrawler = new CafefCrawler();
            //StockRepository objStockRepo = new StockRepository();
            //HistoryRepository objHis = new HistoryRepository();

            //List<stock> stockList = objStockRepo.SelectRecordsByStockExchange(1).ToList();
            //foreach (stock objStock in stockList)
            //{
            //    lblCode.Content = objStock.code;

            //    if (objStock.id_stockexchange == 1)
            //    {
            //        //Lấy dữ liệu khớp lệnh theo lô trong ngày
            //        //và Lấy giá tham chiếu của sàn HOSE
            //        //http://s.cafef.vn/Lich-su-giao-dich-PVT-6.chn?date=17/06/2015
            //        List<history> historyList = objHis.LayDSTheoNgayGiaoDich(objStock.id_stock, dtStart.DateTime).ToList();
            //        foreach (var hisItem in historyList)
            //        {
            //            objCafefCrawler.CapNhatGiaThamChieu(objStock.code, dtStart.DateTime);
            //        }
            //    }
            //}
        }

        private void btnImportHistoryByCode_Click(object sender, RoutedEventArgs e)
        {
            //BaseCrawler objCafefCrawler = new CafefCrawler();
            //StockRepository obj = new StockRepository();

            //var result = obj.Select(txtCode.Text);
            //foreach (stock objStock in result)
            //{
            //    //Lấy dữ liệu price
            //    objCafefCrawler.ImportPriceHisotry(objStock.code, dtStart.DateTime);

            //    //Lấy dữ liệu order
            //    objCafefCrawler.ImportOrderHisotry(objStock.code, dtStart.DateTime);
            //}
        }

        /// <summary>
        /// Update stock data by EzsearchFPT
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdateStockList_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                lblStatus.Content = "Updating ...";
                BaseCrawler fptCrawler = new FptsCrawler();
                fptCrawler.UpdateFPT_IDInfo();
                lblStatus.Content = "Complete!";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btnUpdateStockInfo_Click(object sender, RoutedEventArgs e)
        {
            try 
            {
                lblStatus.Content = "Updating ...";
                BaseCrawler fptCrawler = new FptsCrawler();
                fptCrawler.UpdateEPSInfo();
                lblStatus.Content = "Complete!";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

       
    }
}
